var Py = (function () {
    var methods = {},
        $scroll_top_element;

    methods.selectText = function (element) {
        var doc = document;
        var text = doc.getElementById(element);
        if (doc.body.createTextRange) {
            var range = document.body.createTextRange();
            range.moveToElementText(text);
            range.select();
        } else if (window.getSelection) {
            var selection = window.getSelection();
            var range = document.createRange();
            range.selectNodeContents(text);
            selection.removeAllRanges();
            selection.addRange(range);
        }
    };

    methods.go = function (url, timeout) {

        Py.goUrl = url;

        setTimeout("window.location.href = Py.goUrl;", timeout);
    };

    methods.root = function () {
        return $('#root').length ? $('#root').val() : '/';
    };

    methods.markUpUrl = function (url) {
        url = url.replace(/^\//, '');
        return Py.root() + url;
    };

    methods.scrollTo = function (offset, speed, callback) {
        offset || (offset = 0);
        speed || (speed = 800);
        $('body').animate({scrollTop:offset}, speed, function () {
            callback && callback();
        });
    };

    methods.setGoTop = function ($element) {
        $(document).scroll(function () {
            if ($(document).scrollTop() > ($(window).height() / 5)) {
                if ($element.css('display') == 'none' && $element.queue().length < 1) {
                    $element.slideDown(600);
                }
            } else {
                if ($element.css('display') != 'none' && $element.queue().length < 1) {
                    $element.slideUp(600);
                }
            }
        });

        $element.click(function (event) {
            event.preventDefault();
            methods.scrollTo();
        });
    };


    // Initial functions
    $(document).ready(function () {
        var $goTop = $('#py-go-top').css({
            position:'fixed',
            right:'40px',
            bottom:'50px',
            cursor:'pointer',
            display:'none'
        });
        methods.setGoTop($goTop);

        $('a.no-link').click(function(e) {
            e.preventDefault();
        });
    });

    return methods;
})();
